package com.iptv.grape;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.util.AppLog;

public class BootBroadcastReceiver extends BroadcastReceiver {
    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    private boolean isRegisterReceiver = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();


        GrapeContext.curGrapeContext().screenOff();

        if(action.equals(Intent.ACTION_SCREEN_ON)){
            // 屏幕打开
            AppLog.getCurLog().out("屏幕打开触发");
        }
        if(action.equals(Intent.ACTION_SCREEN_OFF)){
            // 屏幕关闭
            AppLog.getCurLog().out("屏幕关闭触发");
            //GrapeContext.curGrapeContext().screenOff();
        }
        if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
            // HOME按键
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            if(SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)){
                //Toast.makeText(context,"home键触发",Toast.LENGTH_SHORT).show();
                //Log.d(TAG, "home键触发");
                AppLog.getCurLog().out("home键触发");
            }

        }

    }
        //if (intent.getAction().equals(ACTION_BOOT)) { //开机启动完成后，要做的事情

            //mLogger.i(TAG, "ACTION_BOOT BootBroadcastReceiver onReceive(), Do thing!");
            //Intent intent1 = new Intent(context, IptvSettingsService.class);
            //context.startService(intent1);
        //}

    public void registerScreenBroadcastReceiver(Context mContext) {
        if (!isRegisterReceiver) {
            isRegisterReceiver = true;
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            //filter.addAction(Intent.ACTION_SHUTDOWN);
            //filter.addAction(Intent.ACTION_USER_PRESENT);
            mContext.registerReceiver(BootBroadcastReceiver.this, filter);
        }
    }

    public void unRegisterScreenBroadcastReceiver(Context mContext) {
        if (isRegisterReceiver) {
            isRegisterReceiver = false;
            mContext.unregisterReceiver(BootBroadcastReceiver.this);
        }
    }

}
